using System;
using System.Data;
using System.Windows.Forms;
using Company.BLL;
using Company.BLL.GC;
using Company.BLL.DuLieuChuan;
using Janus.Windows.GridEX;
using Company.BLL.KDT;

namespace Company.Interface.GC
{
    public partial class HopDongRegistedForm : BaseForm
    {
        public HopDong HopDongSelected = new HopDong();
        public bool IsBrowseForm = false;
        public bool IsBrowseFormCT = false;
        private DataSet dsRegistedList;
        public HopDongRegistedForm()
        {
            InitializeComponent();
        }

        public void BindData()
        {
            string where = string.Format("MaDoanhNghiep = '{0}'", GlobalSettings.MA_DON_VI);
            dgList.DataSource = new HopDong().SelectCollectionDynamic(where, "NgayKy DESC");
        }
        public void BindHopDongConHan()
        {
            string where = string.Format("MaDoanhNghiep = '{0}'", GlobalSettings.MA_DON_VI);
            where += " AND (NgayHetHan>=getDate() OR NgayGiaHan>=getDate())";
            if (ctrDonViHaiQuan.Ma != "")
                where += string.Format(" and mahaiquan = '{0}' ", ctrDonViHaiQuan.Ma);
          
            dgList.DataSource = new HopDong().SelectCollectionDynamic(where, "");
        }
        //-----------------------------------------------------------------------------------------

        private void khoitao_DuLieuChuan()
        {
            // Đơn vị tính.
            this._DonViTinh = DonViTinh.SelectAll();

            // Đơn vị Hải quan.
            if (this.HopDongSelected.MaHaiQuan.Trim().Length == 0)
                ctrDonViHaiQuan.Ma = GlobalSettings.MA_HAI_QUAN;
            else
                ctrDonViHaiQuan.Ma = this.HopDongSelected.MaHaiQuan;
        }

        //-----------------------------------------------------------------------------------------
        
        private void HopDongRegistedForm_Load(object sender, EventArgs e)
        {
            this.khoitao_DuLieuChuan();

            lblHint.Visible = this.IsBrowseForm;
            ctrDonViHaiQuan.Enabled = !this.IsBrowseForm;

            // Hợp đồng đã đăng ký.
            if (this.IsBrowseForm) BindHopDongConHan();
            else this.BindData();
            // Doanh nghiệp / Đại lý TTHQ.
            dgList.Tables[0].Columns["MaDoanhNghiep"].Visible = GlobalSettings.DAI_LY_TTHQ;
        }

        //-----------------------------------------------------------------------------------------
        
        private void btnGetListFromHQ_Click(object sender, EventArgs e)
        {

            WSForm wsForm = new WSForm();
            wsForm.ShowDialog(this);
            if (!wsForm.IsReady) return;
            try
            {
                this.Cursor = Cursors.WaitCursor;
                HopDong hd = new HopDong();
                hd.updateDatabase(ctrDonViHaiQuan.Ma, GlobalSettings.MA_DON_VI);
                this.Cursor = Cursors.Default;
                ShowMessage("Đã cập nhật thành công",false);
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Default;
                if (ShowMessage("Có lỗi khi thực hiện. Bạn có muốn đưa vào hàng đợi không" + ex.Message, true) == "Yes")
                {                                        
                    HangDoi hd = new HangDoi();
                    hd.ID = 0;
                    hd.MaHaiQuan = ctrDonViHaiQuan.Ma;
                    hd.LoaiToKhai = LoaiToKhai.HOP_DONG;
                    hd.TrangThai = 1;
                    hd.ChucNang = ChucNang.NHAN_THONG_TIN;
                    MainForm.AddToQueueForm(hd);                    
                    MainForm.ShowQueueForm();
                }
            }       

        }

        //-----------------------------------------------------------------------------------------
        
        private void dgList_RowDoubleClick(object sender, RowActionEventArgs e)
        {
            if (e.Row.RowType == RowType.Record)
            {
                string soHD = e.Row.Cells["SoHopDong"].Value.ToString();
                string maHQ = e.Row.Cells["MaHaiQuan"].Value.ToString();
                string maDN = e.Row.Cells["MaDoanhNghiep"].Value.ToString();
                DateTime ngayKy = Convert.ToDateTime(e.Row.Cells["NgayKy"].Value);                
                this.HopDongSelected.SoHopDong = soHD;
                this.HopDongSelected.MaHaiQuan = maHQ;
                this.HopDongSelected.MaDoanhNghiep = maDN;
                this.HopDongSelected.NgayKy = ngayKy;
                this.HopDongSelected.Load();
                
                if (this.IsBrowseForm||this.IsBrowseFormCT)
                {                
                    this.Close();
                }
                else
                {
                    Form[] forms = this.ParentForm.MdiChildren;
                    for (int i = 0; i < forms.Length; i++)
                    {
                        if (forms[i].Name.ToString().Equals(HopDongSelected.SoHopDong))
                        {
                            forms[i].Activate();
                            return;
                        }
                    }
                    HopDongRegistedDetailForm f = new HopDongRegistedDetailForm();
                    f.Name = HopDongSelected.SoHopDong;
                    f.HD = this.HopDongSelected;
                    f.MdiParent = this.ParentForm;
                    f.Show();
                }                    
            }
        }

        private void ctrDonViHaiQuan_ValueChanged(object sender, EventArgs e)
        {
            this.BindData();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgList_LoadingRow(object sender, RowLoadEventArgs e)
        {
            if (e.Row.RowType == RowType.Record)
            {
                if (e.Row.Cells["NgayGiaHan"].Value != DBNull.Value)
                {
                    if (Convert.ToDateTime(e.Row.Cells["NgayGiaHan"].Value).Year == 1900)
                    {
                        e.Row.Cells["NgayGiaHan"].Text = "";
                    }
                }
            }
        }

        private void cmMainHDGC_CommandClick(object sender, Janus.Windows.UI.CommandBars.CommandEventArgs e)
        {
            btnGetListFromHQ_Click(null, null);
        }

        //-----------------------------------------------------------------------------------------
    }
}
